<template>
  <div class="reset-modal">
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px" style="padding: 12px 20px;">
      <el-form-item label="新密码" prop="newPassword">
        <el-input v-model="formData.newPassword" type="password" placeholder="请输入新密码" />
      </el-form-item>
      <el-form-item label="确认密码" prop="confirm">
        <el-input v-model="formData.confirm" type="password" placeholder="请再次输入新密码" />
      </el-form-item>
    </el-form>
    <div class="modal-footer">
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" @click="handleConfirm" :loading="loading">确认</el-button>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'

defineProps({ user: { type: Object, default: null } })
const emit = defineEmits(['close'])

const formRef = ref(null)
const loading = ref(false)
const formData = ref({ newPassword: '', confirm: '' })

const rules = {
  newPassword: [ { required: true, message: '请输入新密码', trigger: 'blur' }, { min: 6, message: '不少于6位', trigger: 'blur' } ],
  confirm: [ { required: true, message: '请确认新密码', trigger: 'blur' }, { validator: (_r, v, cb) => { v !== formData.value.newPassword ? cb(new Error('两次输入不一致')) : cb() }, trigger: 'blur' } ]
}

const handleConfirm = async () => {
  loading.value = true
  try {
    await formRef.value.validate((valid) => {
      if (valid) emit('close', { newPassword: formData.value.newPassword })
    })
  } finally {
    loading.value = false
  }
}

const handleCancel = () => emit('close')
</script>

<style scoped>
.modal-footer { display:flex; justify-content:flex-end; gap:12px; padding: 8px 16px; border-top:1px solid #e4e7ed; }
</style>


